package com.tzy.myassets.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.MyAssets;
import com.tzy.common.entity.ShopCasCoupon;
import com.tzy.myassets.service.MyAssetsService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 我的资产表(优惠红包, 卡券等) 前端控制器
 * </p>
 *
 * @author 刘同学
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/assets")
public class MyAssetsController extends BaseController{
    @Resource
    private MyAssetsService myAssetsService;
/*
    @GetMapping("/info")
    public MyAssets getInfo(HttpServletRequest request) {
        int assetsID = getUid(request);
        MyAssets assets = myAssetsService.getById(assetsID);
        return assets;
    }
*/
    /**
     *
     * @param request
     * @param myAssets
     * @return
     */
    @PostMapping("/add")
    public WebResult assetsAdd(HttpServletRequest request,@RequestBody MyAssets myAssets) {
//        MyAssets myAssets = new MyAssets();
        //用户id
        int userId = getUserId(request);
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取系统时间+2天
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 2);
        //输出时间
        myAssets.setUserId(userId);//用户id
        myAssets.setEffectiveDate(Timestamp.valueOf(df.format(new Date())));// new Date()为获取当前系统时间
        myAssets.setDeadline(Timestamp.valueOf(df.format(c.getTime())));//c.getTime()为获取当前系统时间+2天
//        myAssets.setCashCouponName(shopName);//红包名
//        myAssets.setThreshold(threshold);//红包门槛
        myAssets.setExpired(false);//是否过期
//        myAssets.setPreferentialAmount(discount);//红包金额
        if( myAssets.getShopId()>0){
            myAssets.setShopId(myAssets.getShopId());//商家id
            myAssets.setIntroductionToUse("可与其他活动优惠同时享受，在线支付专享。限指定门店使用。");//介绍
        }else{
            myAssets.setShopId(0);//商家id
            myAssets.setIntroductionToUse("限商品现价+打包费-所有优惠金额满20元使用（不含配送费）。限非到店自取订单，限天天神券商家，限登录和收餐手机号使用");
        }
        boolean jg = myAssetsService.save(myAssets);
        if(jg){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("jg","添加成功");
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("jg","添加失败");
    }
//    /**
//     *
//     * @param request
//     * @param shopName
//     * @return
//     */
//    @GetMapping("/assetsAdd")
//    public WebResult assetsAdd(HttpServletRequest request,
//                               @RequestParam("shopName") String shopName,
//                               @RequestParam("shopId") int shopId,
//                               @RequestParam("discount") int discount,
//                               @RequestParam("threshold") int threshold) {
//        MyAssets myAssets = new MyAssets();
//        //用户id
//        int uid = getUserId(request);
//        //设置日期格式
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        //获取系统时间+2天
//        Calendar c = Calendar.getInstance();
//        c.add(Calendar.DAY_OF_MONTH, 2);
//        //输出时间
//        myAssets.setUserId(uid);//用户id
//        myAssets.setEffectiveDate(Timestamp.valueOf(df.format(new Date())));// new Date()为获取当前系统时间
//        myAssets.setDeadline(Timestamp.valueOf(df.format(c.getTime())));//c.getTime()为获取当前系统时间+2天
//        myAssets.setCashCouponName(shopName);//红包名
//        myAssets.setThreshold(threshold);//红包门槛
//        myAssets.setExpired(false);//是否过期
//        myAssets.setPreferentialAmount(discount);//红包金额
//        if(shopId>0){
//            myAssets.setShopId(shopId);//商家id
//            myAssets.setIntroductionToUse("可与其他活动优惠同时享受，在线支付专享。限指定门店使用。");//介绍
//        }else{
//            myAssets.setShopId(0);//商家id
//            myAssets.setIntroductionToUse("限商品现价+打包费-所有优惠金额满20元使用（不含配送费）。限非到店自取订单，限天天神券商家，限登录和收餐手机号使用");
//        }
//        boolean jg = myAssetsService.save(myAssets);
//        if(jg){
//            return new WebResult(WebResultMsgCode.SUCCESS).putData("jg","添加成功");
//        }
//        return new WebResult(WebResultMsgCode.FAIL).putData("jg","添加失败");
//    }

    /**
     * 删除数据
     * @param id 红包编号
     * @return
     */
    @DeleteMapping("/del")
    public WebResult assetsDel(@RequestParam Integer id){
        if (id!=null){
            int jg = myAssetsService.assetsDel(id);
            if (jg>0){
                return new WebResult(WebResultMsgCode.SUCCESS).putData("delete","删除成功");
            }
            return new WebResult(WebResultMsgCode.FAIL).putData("delete","删除失败");
        }
        return null;
    }

    /**
     * 更新数据
     * @param deadline 到期时间
     * @return
     */
    @GetMapping("/upd")
    //添加定时任务-- 2天执行一次
    @Scheduled(cron = "* * * 0/2 * *")
    public WebResult assetsUpd(HttpServletRequest request,@RequestParam Timestamp deadline){
        int assetsID = getUserId(request);
        int jg = myAssetsService.assetsUpd(assetsID, deadline);
        if (jg>0){
            return new WebResult(WebResultMsgCode.SUCCESS);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }

    /**
     * 查询数据
     * @return
     */
    @GetMapping("/sel")
    public WebResult assetsSel(HttpServletRequest request){
//         @RequestParam int id
        int assetsID = getUserId(request);
        List<MyAssets> list = myAssetsService.assetsSel(assetsID);
        if (list!=null||list.isEmpty()){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("select",list);
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("select","加载失败");
    }

    /**
     * 查询数据
     * @return
     */
    @GetMapping("/selShop")
    public WebResult assetsSelShop(HttpServletRequest request){
//          @RequestParam int id
        int userId = getUserId(request);
        List<MyAssets> list = myAssetsService.assetsSelShop(userId);
        if (list!=null||list.isEmpty()){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("selectShop",list);
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("selectShop","加载失败");
    }

    /**
     * 分页查询
     * @param pageNum
     * @return
     */
    @GetMapping("/page")
    public WebResult pageTest(@RequestParam(defaultValue = "1") Integer pageNum){
        Page<MyAssets> page = myAssetsService.assetsPage(pageNum, 5);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("page",page);
    }

    /**
     * 查询总记录数
     * @return
     */
    @GetMapping("/total")
    public WebResult findTotal(){
        int jg = myAssetsService.findTotal();
        if(jg>0) {
            return new WebResult(WebResultMsgCode.SUCCESS).putData("total", jg);
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("total","加载失败");
    }

    /**
     * 查询近7天已使用的优惠券
     * @param request
     * @return
     */
    @GetMapping("/useDate")
    public WebResult nearlySevenUseDate(HttpServletRequest request){
        int userId = getUserId(request);
        List<MyAssets> list = myAssetsService.nearlySevenUseDate(userId);
        if (list!=null){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("useDate",list);
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("useDate","加载失败");
    }

    /**
     * 更新use
     * @param request
     * @return
     */
    public WebResult updateUse(HttpServletRequest request){
        int userId = getUserId(request);
        int jg = myAssetsService.updateUse(userId);
        if(jg>0){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("updateUse","使用成功");
        }
        return new WebResult(WebResultMsgCode.SUCCESS).putData("updateUse","使用失败");
    }
}
